package com.app.outdoor.pojo;

import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.app.outdoor.adapter.GroupEntity;

import ReadWriteXML.XMLBuilder;

public class TinNhanPOJO {
	private int id;
	private int idNguoiGui;
	private int idNguoiNhan;
	private String noiDung;
	private int tinhTrang;
	private long thoiGian;
	private int loai;

	public int getIdNguoiGui() {
		return idNguoiGui;
	}

	public void setIdNguoiGui(int iDNguoiGui) {
		this.idNguoiGui = iDNguoiGui;
	}

	public int getIdNguoiNhan() {
		return idNguoiNhan;
	}

	public void setIdNguoiNhan(int idNguoiNhan) {
		this.idNguoiNhan = idNguoiNhan;
	}

	public String getNoiDung() {
		return noiDung;
	}

	public void setNoiDung(String noiDung) {
		this.noiDung = noiDung;
	}

	public int getTinhTrang() {
		return tinhTrang;
	}

	public void setTinhTrang(int tinhTrang) {
		this.tinhTrang = tinhTrang;
	}

	public long getThoiGian() {
		return thoiGian;
	}

	public void setThoiGian(long thoiGian) {
		this.thoiGian = thoiGian;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getLoai() {
		return loai;
	}

	public void setLoai(int loai) {
		this.loai = loai;
	}

	public Element createXMLElement(Document doc) {
		Element e = doc.createElement("TinNhan");
		e.setAttribute("ID", id + "");
		e.setAttribute("IDNguoiGui", idNguoiGui + "");
		e.setAttribute("IDNguoiNhan", idNguoiNhan + "");
		e.setAttribute("NoiDung", noiDung);
		e.setAttribute("TinhTrang", tinhTrang + "");
		e.setAttribute("ThoiGian", thoiGian + "");
		e.setAttribute("Loai", this.loai + "");
		return e;
	}

	public boolean parse(String xml) {
		boolean rs = false;
		try {
			Document doc = XMLBuilder.loadXMLFromString(xml);
			NodeList nodelist = doc.getElementsByTagName("TinNhan");
			if (nodelist != null) {
				Element element = (Element) nodelist.item(0);
				this.id = Integer.parseInt(element.getAttribute("ID"));
				this.idNguoiGui = Integer.parseInt(element.getAttribute("IDNguoiGui"));
				this.idNguoiNhan = Integer.parseInt(element.getAttribute("IDNguoiNhan"));
				this.noiDung = element.getAttribute("NoiDung");
				this.thoiGian = Long.parseLong(element.getAttribute("ThoiGian"));
				this.loai = Integer.parseInt(element.getAttribute("Loai"));
				rs = true;
			}
		} catch (Exception e) {
			rs = false;
		}
		return rs;
	}

	public void parse(Element element) {
		this.id = Integer.parseInt(element.getAttribute("ID"));
		this.idNguoiGui = Integer.parseInt(element.getAttribute("IDNguoiGui"));
		this.idNguoiNhan = Integer.parseInt(element.getAttribute("IDNguoiNhan"));
		this.noiDung = element.getAttribute("NoiDung");
		this.thoiGian = Long.parseLong(element.getAttribute("ThoiGian"));
		this.loai = Integer.parseInt(element.getAttribute("Loai"));
	}

	public static ArrayList<TinNhanPOJO> parseArray(String xml) {
		ArrayList<TinNhanPOJO> rs = null;
		try {
			Document doc = XMLBuilder.loadXMLFromString(xml);
			NodeList groupList = doc.getElementsByTagName("TinNhan");

			if (groupList != null) {
				rs = new ArrayList<TinNhanPOJO>();
				for (int i = 0; i < groupList.getLength(); i++) {

					TinNhanPOJO tn = new TinNhanPOJO();
					Element element = (Element) groupList.item(i);
					tn.parse(element);
					rs.add(tn);
				}
			}
		} catch (Exception e) {
			rs = null;
		}
		return rs;
	}
}
